<?php
class Model_Members extends Zend_Db_Table_Abstract
{
    protected $_name = 'members';

	function numRows($where = null)
    {   
        $select = $this->_db->select();
        $select->from($this->_name, "COUNT(*)", $this->_schema);
        // the WHERE clause
        $where = (array) $where;
        foreach ($where as $key => $val) {
            // is $key an int?
            if (is_int($key)) {
                // $val is the full condition
                $select->where($val);
            } else {
                // $key is the condition with placeholder,
                // and $val is quoted into the condition              
                $select->where($key, $val);           
            }          
        }
        // return the results
        $stmt = $this->_db->query($select);

        $data = $stmt->fetchColumn(0);       
        return $data;  
    }
    
	public function getPaginator($filters = array(), $sort = null, $page = 1, $rowCount = null)
    {
        // get select query
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($sort != null)
            $select->order ( $sort );
            
        // get adapter
        $adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
        
        // get paginator
        $paginator = new Zend_Paginator ( $adapter );
        $paginator->setItemCountPerPage ( $rowCount );
        $paginator->setCurrentPageNumber ( $page );
        
        return $paginator;
    }

    public function getList($filters = array(), $sort = null, $page = null, $rowCount = null)
    { 
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($page != null)
            $select->limitPage ( $page, $rowCount );
        
        if ($sort != null)
            $select->order ( $sort );
       
        return $this->fetchAll ( $select );
    }
    
    // Get information of user 
    function getCurrentUserInfor(){
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity())
            $userInfo = $auth->getStorage()->read() ;
        else
            return null;
              
        if($this->find ( $userInfo->id )->count())
        {
            return $this->find ( $userInfo->id )->current();
        }
        
        // User don't exist in database
        return null;
    }
    
    public function createUser($username, $password, $firstName, $lastName, $role)
    {
        // create a new row
        $rowUser = $this->createRow ();
        if ($rowUser)
        {
            // update the row values
            $rowUser->username = $username;
            $rowUser->password = md5 ( $password );
            $rowUser->first_name = $firstName;
            $rowUser->last_name = $lastName;
            $rowUser->role = $role;
            $rowUser->save ();
            //return the new user
            return $rowUser;
        } else
        {
            throw new Zend_Exception ( "Could not create user! " );
        }
    }

    public static function getUsers()
    {
        $userModel = new self ( );
        $select = $userModel->select ();
        $select->order ( array ('last_name', 'first_name' ) );
        return $userModel->fetchAll ( $select );
    }

    public function updateUser($id, $username, $firstName, $lastName, $role)
    {
        // fetch the user's row
        $rowUser = $this->find ( $id )->current ();
        
        if ($rowUser)
        {
            // update the row values
            $rowUser->username = $username;
            $rowUser->first_name = $firstName;
            $rowUser->last_name = $lastName;
            $rowUser->role = $role;
            $rowUser->save ();
            //return the updated user
            return $rowUser;
        } 
        else
        {
            throw new Zend_Exception ( "User update failed.  User not found!" );
        }
    }

    public function updatePassword($id, $password)
    {
        // fetch the user's row
        $rowUser = $this->find ( $id )->current ();
        
        if ($rowUser)
        {
            //update the password
            $rowUser->password = md5 ( $password );
            $rowUser->save ();
        } else
        {
            throw new Zend_Exception ( "Password update failed.  User not found!" );
        }
    }

    public function deleteUser($id)
    {
        // fetch the user's row
        $rowUser = $this->find ( $id )->current ();
        if ($rowUser)
        {
            $rowUser->delete ();
        } else
        {
            throw new Zend_Exception ( "Could not delete user.  User not found!" );
        }
    }

}
